package com.niit.activity.controller;

import com.niit.activity.entity.Comment;
import com.niit.activity.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

        /**
     * 根据活动ID获取评论列表
     * @param activityId 活动ID
     * @return 评论列表
     */
    @GetMapping("/activity/{activityId}")
    public List<Comment> getCommentsByActivityId(@PathVariable Long activityId) {
        return commentService.getCommentsByActivityId(activityId);
    }

    /**
     * 根据父评论ID获取子评论列表
     * @param parentId 父评论ID
     * @return 子评论列表
     */
    @GetMapping("/parent/{parentId}")
    public List<Comment> getCommentsByParentId(@PathVariable Long parentId) {
        return commentService.getCommentsByParentId(parentId);
    }

    /**
     * 创建新评论
     * @param comment 评论对象
     * @return 创建成功的评论对象或错误响应
     */
    @PostMapping
    public ResponseEntity<Comment> createComment(@RequestBody Comment comment) {
        try {
            commentService.addComment(comment);
            return ResponseEntity.ok(comment);
        } catch (Exception e) {
            // 记录异常并返回500错误
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }

}